package com.lineage.server.command.executor;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_PacketBoxGm;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Recall implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Recall.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1Recall();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      Collection<L1PcInstance> targets = null;
      if (arg.equalsIgnoreCase("all")) {
        targets = World.get().getAllPlayers();
      } else {
        targets = new ArrayList<>();
        L1PcInstance tg = World.get().getPlayer(arg);
        if (tg == null) {
          int mode = 2;
          pc.sendPackets((ServerBasePacket)new S_PacketBoxGm(pc, 2));
          return;
        } 
        targets.add(tg);
      } 
      Iterator<L1PcInstance> iterator = targets.iterator();
      while (iterator.hasNext()) {
        L1PcInstance target = iterator.next();
        if (target.isGm())
          continue; 
        L1Teleport.teleportToTargetFront((L1Character)target, (L1Character)pc, 2);
      } 
    } catch (Exception e) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
}
